function ExportTabletoExcelInOldIE(table, FileName) {
    //table variable contains the html to be exported to Excel
    var sa = null;
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");
    if (msie > 0) // If old Internet Explorer including IE 8
    {
        //make sure you have an empty div with id of iframeDiv in your page
        document.getElementById('iframeDiv').innerHTML = '<iframe id="txtArea1" style="display:none"></iframe>';
        txtArea1.document.open("txt/html", "replace");
        txtArea1.document.write(table);
        txtArea1.document.close();
        txtArea1.focus();
        sa = txtArea1.document.execCommand("SaveAs", true, FileName + ".xls");
        document.getElementById('iframeDiv').innerHTML = "";
    } else {
        window.navigator.msSaveBlob(new Blob([table], {
            type: 'application/vnd.ms-excel'
        }), FileName + ".xls");
    }
    return (sa);
}

var toExcel = function(title, FileName, data) {

    var JSONData = getData(data);
    var ShowLabel = getTit(title);

    // console.log(JSONData)
    //先转化json  
    var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;

    var excel = '<table>';

    //设置表头  
    var row = "<tr>";
    for (var i = 0, l = ShowLabel.length; i < l; i++) {
        row += "<td>" + ShowLabel[i].value + '</td>';
    }

    //换行  
    excel += row + "</tr>";

    //设置数据
    if (arrData.length > 0 && arrData[0].length == 1) {

        excel += '<tr>';

        for (var i = 0; i < arrData.length; i++) {
            excel += '<td>' + arrData[i][0].value + "</td>";
        }
        excel += '</tr>';
    } else {

        for (var i = 0; i < arrData.length; i++) {
            var rowData = "<tr>";

            for (var index in arrData[i]) {
                var value = arrData[i][index].value === "." ? "" : arrData[i][index].value;
                rowData += '<td>' + value + '</td>';
            }

            excel += rowData + "</tr>";
        }
    }

    excel += "</table>";

    var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
    excelFile += '; charset=UTF-8">';
    excelFile += "<head>";
    excelFile += "<!--[if gte mso 9]>";
    excelFile += "<xml>";
    excelFile += "<x:ExcelWorkbook>";
    excelFile += "<x:ExcelWorksheets>";
    excelFile += "<x:ExcelWorksheet>";
    excelFile += "<x:Name>";
    excelFile += "{worksheet}";
    excelFile += "</x:Name>";
    excelFile += "<x:WorksheetOptions>";
    excelFile += "<x:DisplayGridlines/>";
    excelFile += "</x:WorksheetOptions>";
    excelFile += "</x:ExcelWorksheet>";
    excelFile += "</x:ExcelWorksheets>";
    excelFile += "</x:ExcelWorkbook>";
    excelFile += "</xml>";
    excelFile += "<![endif]-->";
    excelFile += "</head>";
    excelFile += "<body>";
    excelFile += excel;
    excelFile += "</body>";
    excelFile += "</html>";

    var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);

    var ua = window.navigator.userAgent;
    if (ua.indexOf("Trident") > 0 || ua.indexOf("MSIE") > 0) {
        ExportTabletoExcelInOldIE(excelFile, FileName);
    } else {
        var link = document.createElement("a");
        link.href = uri;

        link.setAttribute('style', "visibility:hidden");
        link.setAttribute('download', FileName + ".xls");
        link.setAttribute('target', "_blank");

        document.body.appendChild(link);
        // link.click(window.open(uri));
        // link.click(window.location.href = uri);  
        link.click();
        document.body.removeChild(link);
    }
}

function getTit(d) {
    var data = [],
        i = 0;
    for (i; i < d.length; i++) {
        data.push({
            "value": d[i],
            "type": "ROW_HEADER_HEADER",
            "datatype": "string"
        })
    }
    return data;
}

function getData(d) {
    var data = [],
        i = 0;
    for (i; i < d.length; i++) {
        var ds = [];
        for (var j = 0; j < d[i].length; j++) {
            ds.push({
                "value": d[i][j],
                "type": "ROW_HEADER"
            })
        }
        data.push(ds)
    }
    return data;

}

// var data = {
//             "title":[
//                 {"value":"购买渠道", "type":"ROW_HEADER_HEADER", "datatype":"string"}, 
//                 {"value":"数量", "type":"ROW_HEADER_HEADER", "datatype":"string"},
//                 {"value":"占比", "type":"ROW_HEADER_HEADER", "datatype":"string"}
//                 ],
//             "data":[
//                      [
//                     {"value":"只在POS购买", "type":"ROW_HEADER"},
//                     {"value":state.PosValue, "type":"ROW_HEADER"},
//                     {"value":state.PosPct, "type":"ROW_HEADER"}
//                 ]]};
export default toExcel;